package com.example.demo.dao;

import com.example.demo.model.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;


@Component
public class SpringJdbcTemplate {
    @Autowired
    private JdbcTemplate template;

    @Autowired
    private NamedParameterJdbcTemplate namedTemplate;

    public  void insert(Test test) throws ClassNotFoundException {
        String sql = "INSERT INTO test_2(name,math_score,chinese_score,english_score,computer_score) VALUES (:name,:mathScore,:chineseScore,:englishScore,:computerScore)";
        namedTemplate.update(sql,new BeanPropertySqlParameterSource(test));
   }
    public  void change(Test test) throws ClassNotFoundException {
        String sql = "update test_2 set name=:name,math_score=:mathScore,chinese_score=:chineseScore,english_score=:englishScore,computer_score=:computerScore where id=:id";
        namedTemplate.update(sql,new BeanPropertySqlParameterSource(test) );
    }
    public  void delete(Test test) throws ClassNotFoundException {
        String sql = "delete from test_2 where id=:id ";
        namedTemplate.update(sql,new BeanPropertySqlParameterSource(test) );
    }
    public  List get(Test test) throws ClassNotFoundException {
        String sql = "select * from test_2 where id=:id";
//        List list=namedTemplate.queryForList(sql,new BeanPropertySqlParameterSource(test));
        List<Test> lst = namedTemplate.query(sql,new BeanPropertySqlParameterSource(test),new BeanPropertyRowMapper(Test.class));
        return lst;
    }


}